package com.cat.dataStructure03;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/first-completely-painted-row-or-column/
 * @create 2025/11/3 15:40
 * @since JDK17
 */

public class Solution06 {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int k = arr.length, n = mat.length, m = mat[0].length;
        int[] row = new int[n], col = new int[m];
        Map<Integer, Long> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map.put(mat[i][j], (long)i * m + j);
            }
        }
        for (int i = 0; i < k; i++) {
            Long p = map.get(arr[i]);
            int x = (int)(p / m), y = ((int) (p % m));
            row[x]++;
            col[y]++;
            if (row[x] == m || col[y] == n) {
                return i;
            }
        }
        return -1;
    }
}
